package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"yklx5/cache"
	"yklx5/models"
	"yklx5/pkg"
)

type WWW struct {
	beego.Controller
}
type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (w *WWW) Deng() {
	username := w.GetString("username")
	password := w.GetString("password")
	if username == "" || password == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名和密码不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var err error
	var data models.User
	err = data.Dengs(username, &data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名不正确",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	DataKey := "dataid" + username
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, DataKey).Int()
	if cacheCount >= 3 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误3次,请稍后再试",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	if data.Password != pkg.Md5str1(password) {
		cache.Rdb.Incr(cache.Ctx, DataKey)
		if cacheCount == 2 {
			cache.Rdb.Set(cache.Ctx, DataKey, 3, time.Hour*2)
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "密码错误三次,请稍后再试",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "登录成功",
		Data: data,
	}
	w.ServeJSON()

}
func (w *WWW) RoleQuanAdd() {
	qid, _ := w.GetInt("qid", 0)
	rid, _ := w.GetInt("rid", 0)
	if qid == 0 || rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "权限ID和角色ID不为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	data := models.RoleQuan{
		Qid: qid,
		Rid: rid,
	}
	err := data.RoleQuanAdd(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
}
func (w *WWW) UserRoleList() {
	uid, _ := w.GetInt("uid", 0)
	if uid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户ID不为零",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var roless models.Role
	err := roless.Rolelist(uid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: roless,
	}
	w.ServeJSON()
	return

}
func (w *WWW) RoleQuanList() {
	rid, _ := w.GetInt("rid", 0)
	if rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID不为零",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var quans models.Quan
	err := quans.Quanlist(rid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: quans,
	}
	w.ServeJSON()
	return
}
func (w *WWW) CompansAdd() {
	compansname := w.GetString("compansname")
	if compansname == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流名称不为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	data := models.Compans{
		Compansname: compansname,
	}
	err := data.CompansAdds(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return

}
func (w *WWW) OrderAdd() {
	ordersname := w.GetString("ordersname")
	cid, _ := w.GetInt("cid", 0)
	tid, _ := w.GetInt("tid", 0)
	if ordersname == "" || cid == 0 || tid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "快递单号,物流名称ID,物流类型ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	data := models.Order{
		Ordersname: ordersname,
		Cid:        cid,
		Tid:        tid,
	}
	err := data.OrderAdds(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
	return
}
func (w *WWW) List() {
	var err error
	var listss []map[string]interface{}
	cachekey := "redisID"
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cachekey).Result()
	if cacheCount == "" {
		err = models.DB.Table("orders").
			Joins("LEFT JOIN compans ON compans.id = orders.cid").
			Joins("LEFT JOIN types ON types.id = orders.tid").
			Select("orders.id,orders.ordersname,compans.compansname,types.typesname").
			Scan(&listss).Error
		DataJson, _ := json.Marshal(&listss)
		cache.Rdb.Set(cache.Ctx, cachekey, DataJson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheCount), &listss)
	}
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: listss,
	}
	w.ServeJSON()
	return
}
func (w *WWW) Dan() {
	id, _ := w.GetInt("id", 0)
	var err error
	var listss []map[string]interface{}
	cachekey := "redisIDs" + strconv.Itoa(id)
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cachekey).Result()
	if cacheCount == "" {
		err = models.DB.Table("orders").
			Joins("LEFT JOIN compans ON compans.id = orders.cid").
			Joins("LEFT JOIN types ON types.id = orders.tid").
			Select("orders.id,orders.ordersname,compans.compansname,types.typesname").
			Where("orders.id=?", id).Scan(&listss).Error
		DataJson, _ := json.Marshal(&listss)
		cache.Rdb.Set(cache.Ctx, cachekey, DataJson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheCount), &listss)
	}
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: listss,
	}
	w.ServeJSON()
	return
}
